/// 聊天消息类型
enum MessageType {
  text,
  image,
  voice,
  file,
}

/// 消息发送者
enum MessageSender {
  user, // 用户
  ai, // AI
}

/// 聊天消息模型
class ChatMessage {
  final String id;
  final MessageSender sender;
  final MessageType type;
  final String content;
  final String? imageUrl;
  final String? fileUrl;
  final String? fileName; // 文件名
  final DateTime time;
  final bool isThinking; // 是否正在思考中

  ChatMessage({
    required this.id,
    required this.sender,
    required this.type,
    required this.content,
    this.imageUrl,
    this.fileUrl,
    this.fileName,
    DateTime? time,
    this.isThinking = false,
  }) : time = time ?? DateTime.now();

  /// 创建用户文本消息（可包含图片或文件）
  factory ChatMessage.userText(String content, {String? imageUrl, String? fileUrl, String? fileName}) {
    MessageType type = MessageType.text;
    if (imageUrl != null) {
      type = MessageType.image;
    } else if (fileUrl != null) {
      type = MessageType.file;
    }
    
    return ChatMessage(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      sender: MessageSender.user,
      type: type,
      content: content,
      imageUrl: imageUrl,
      fileUrl: fileUrl,
      fileName: fileName,
    );
  }

  /// 创建用户图片消息
  factory ChatMessage.userImage(String imageUrl, {String? text}) {
    return ChatMessage(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      sender: MessageSender.user,
      type: MessageType.image,
      content: text ?? '',
      imageUrl: imageUrl,
    );
  }

  /// 创建用户语音消息
  factory ChatMessage.userVoice(String voiceUrl, {int duration = 0}) {
    return ChatMessage(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      sender: MessageSender.user,
      type: MessageType.voice,
      content: '${duration}s', // 存储时长
      fileUrl: voiceUrl,
    );
  }

  /// 创建用户文件消息
  factory ChatMessage.userFile(String fileUrl, {String? fileName, String? text}) {
    return ChatMessage(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      sender: MessageSender.user,
      type: MessageType.file,
      content: text ?? '',
      fileUrl: fileUrl,
      fileName: fileName ?? fileUrl.split('/').last,
    );
  }

  /// 创建 AI 思考中消息
  factory ChatMessage.aiThinking() {
    return ChatMessage(
      id: 'thinking_${DateTime.now().millisecondsSinceEpoch}',
      sender: MessageSender.ai,
      type: MessageType.text,
      content: '思考中...',
      isThinking: true,
    );
  }

  /// 创建 AI 回复消息
  factory ChatMessage.aiReply(String content) {
    return ChatMessage(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      sender: MessageSender.ai,
      type: MessageType.text,
      content: content,
    );
  }

  /// 复制并更新内容
  ChatMessage copyWith({
    String? content,
    bool? isThinking,
  }) {
    return ChatMessage(
      id: id,
      sender: sender,
      type: type,
      content: content ?? this.content,
      imageUrl: imageUrl,
      fileUrl: fileUrl,
      fileName: fileName,
      time: time,
      isThinking: isThinking ?? this.isThinking,
    );
  }
}
